package band.wukong.practice.java.core.topic.lambda;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.List;

/**
 * @author wukong(wukonggg@139.com)
 */
public class LambdaExamples {

    public static void main(String[] args) {
        e0();
    }
    private static void e0() {

    }

    private static void e1() {
        //Before Java 8:
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Before Java8, too much code for too little to do");
            }
        }).start();

        //Java 8 way 1:
        new Thread( () -> System.out.println("In Java8, Lambda expression rocks !") ).start();

        //Java 8 way 2:
        new Thread( () -> {
            System.out.println("In Java8, Lambda expression rocks and roll !!!");
        }).start();
    }

    private static void e2() {
        // Before Java 8:
        JButton show = new JButton("Show");
        show.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Event handling without lambda expression is boring");
            }
        });
        // Java 8 way:
        show.addActionListener((e) -> {
            System.out.println("Light, Camera, Action !! Lambda expressions Rocks");
        });
    }

    private static void e3() {
        List<String> features = Arrays.asList("Lambdas", "Default Method", "Stream API", "Date and Time API");

        //Prior Java 8 :
        for (String f : features) {
            System.out.println(f);
        }
        //In Java 8 way 1:
        features.forEach(f -> System.out.println(f));

        // 用java8的方法引用更好，方法引用由::(双冒号)操作符来完成,看起来像c++中的作用域操作符
        features.forEach(System.out::println);
    }


}
